import {createStore} from 'vuex';

// 创建 Vuex Store
export default createStore({
    state: {
        selectedChat: null, // 当前选中的对话
        chatGroups: [] // 对话列表数据
    },
    mutations: {
        // 设置选中的对话
        setSelectedChat(state, chat) {
            state.selectedChat = chat;

            // 重置所有项的选中状态
            state.chatGroups.forEach((g) => {
                g.chats.forEach((c) => {
                    c.isActive = false;
                });
            });

            // 设置当前项为选中状态
            chat.isActive = true;
        },
        // 更新对话标题
        updateChatTitle(state, newTitle) {
            if (state.selectedChat) {
                state.selectedChat.title = newTitle;
            }
        },
        // 获取菜单数据
        getChatList(state, data) {
            state.chatGroups = data;
        },
    },
    actions: {
        // 选择对话
        selectChat({commit}, chat) {
            commit('setSelectedChat', chat);
        },
        // 更新标题
        updateTitle({commit}, newTitle) {
            commit('updateChatTitle', newTitle);
        }
    },
    getters: {
        // 获取选中的对话
        selectedChat: (state) => state.selectedChat,
        // 获取对话列表
        chatGroups: (state) => state.chatGroups,
    },
});
